﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_Admin_manageClients : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.lb_noResultsFound.Visible = false;

        if (!IsPostBack)
        {
            doSearch(1);
        }
       
    }
    protected void gv_clientList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            /*
            string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[4].Text);
            e.Row.Cells[3].Text = "<ul>" + HttpUtility.HtmlDecode(e.Row.Cells[3].Text) + "</ul>";
            e.Row.Cells[4].Text = "<ul>"+decodedText+"</ul>";
            e.Row.Cells[5].Text = "<ul>" + HttpUtility.HtmlDecode(e.Row.Cells[5].Text) + "</ul>";
            
            
            //e.Row.ForeColor = System.Drawing.Color.DarkGray;
            e.Row.Cells[3].ControlStyle.ForeColor = System.Drawing.Color.Black;
            e.Row.Cells[4].ForeColor = System.Drawing.Color.Black;
            this.gv_clientList.RowStyle.ForeColor = System.Drawing.Color.Black;
             * */
            //http://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx

            int clientId = Convert.ToInt32(this.gv_clientList.DataKeys[e.Row.RowIndex].Value.ToString());
            GridView gv_clinetRequests = e.Row.FindControl("gv_clinetRequests") as GridView;
            GridView gv_clinetDetailLinks = e.Row.FindControl("gv_clinetDetailLinks") as GridView;

            List<SearchRow> dataSource = (List<SearchRow>)gv_clientList.DataSource;
            foreach (SearchRow searchRow in dataSource)
            {
                if (searchRow.clientId == clientId)
                {
                    gv_clinetRequests.DataSource = searchRow.clientRequests;
                    gv_clinetRequests.DataBind();

                    gv_clinetDetailLinks.DataSource = searchRow.clientDetailLinks;
                    gv_clinetDetailLinks.DataBind();

                }
            }

        }
        
    }

    protected void tbn_searchClient_Click(object sender, ImageClickEventArgs e)
    {
        doSearch(1);
    }


    private void doSearch(int pageNumber)
    {
        string input = this.tb_searchClient.Text;
        string searchType = this.ddl_listType.SelectedValue;
        Dictionary<string, Object> critera = new Dictionary<string, Object>();
        critera.Add("input", input);
        critera.Add("searchType", searchType);
       
        AdminSearch search = new AdminSearch();
        List<SearchRow> list = search.getData(critera);

        //Set no. of rows to show in each page
        PagingMaker<SearchRow> pagingMaker = new PagingMaker<SearchRow>(5);
        List<SearchRow> pageList = pagingMaker.getPageDate(list, pageNumber);
        this.tb_pageNo.Text = Convert.ToString(pagingMaker.currentPage);
        this.totalRowCount.Text = Convert.ToString(pagingMaker.totalRowCount);
        this.totalPages.Text = Convert.ToString(pagingMaker.totalPages);

        if ("0".Equals(this.totalRowCount.Text))
        {
            this.lb_noResultsFound.Visible = true;

        }else{
            this.lb_noResultsFound.Visible = false;
        }

        if (pagingMaker.currentPage <= 1)
        {
            tbn_previous.Enabled = false;
        }
        else
        {
            tbn_previous.Enabled = true;
        }

        if (pagingMaker.currentPage >= pagingMaker.totalPages)
        {
            tbn_next.Enabled = false;
        }
        else
        {
            tbn_next.Enabled = true;
        }

        this.gv_clientList.DataSource = pageList;
        this.gv_clientList.DataBind();
    }


    protected void tbn_previous_Click(object sender, EventArgs e)
    {
        int inputPage = Convert.ToInt32(this.tb_pageNo.Text);
        doSearch(inputPage-1);
    }
    protected void tbn_next_Click(object sender, EventArgs e)
    {
        int inputPage = Convert.ToInt32(this.tb_pageNo.Text);
        doSearch(inputPage + 1);
    }

    protected void tb_pageNo_TextChanged(object sender, EventArgs e)
    {
        int inputPage = Convert.ToInt32(this.tb_pageNo.Text);
        doSearch(inputPage);
    }


    protected void gv_clinetDetailLinks_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.BorderStyle = BorderStyle.None;
        }

    }

    protected void gv_clinetRequests_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.BorderStyle = BorderStyle.None;
        }

    }
}